package com.ruoyi.yard.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.ServiceException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yard.domain.CompWarehouseInfo;
import com.ruoyi.yard.service.ICompWarehouseInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 构件库存信息Controller
 * 
 * @author ruoyi
 * @date 2022-09-02
 */
@RestController
@RequestMapping("/yard/compWarehouseInfo")
public class CompWarehouseInfoController extends BaseController
{
    @Autowired
    private ICompWarehouseInfoService compWarehouseInfoService;

    /**
     * 查询构件库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('yard:compWarehouseInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompWarehouseInfo compWarehouseInfo)
    {

        if (compWarehouseInfo.getBeginFloor()==null && compWarehouseInfo.getEndFloor()==null){

        }else if (compWarehouseInfo.getBeginFloor()==0 && compWarehouseInfo.getEndFloor()==0){
            compWarehouseInfo.setBeginFloor(null);
            compWarehouseInfo.setEndFloor(null);
        }else if (compWarehouseInfo.getBeginFloor()< compWarehouseInfo.getEndFloor()){
            throw new ServiceException("开始楼层不能大于结束楼层");
        }


        if (compWarehouseInfo.getBeginWarehousedTime()==null && compWarehouseInfo.getEndWarehousedTime()==null){

        }else if (compWarehouseInfo.getBeginWarehousedTime().compareTo(compWarehouseInfo.getEndWarehousedTime())<0){
            throw new ServiceException("开始入库时间不能大于开始入库时间");
        }

        startPage();
        List<CompWarehouseInfo> list = compWarehouseInfoService.selectCompWarehouseInfoList(compWarehouseInfo);
        return getDataTable(list);
    }

    /**
     * 导出构件库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('yard:compWarehouseInfo:export')")
    @Log(title = "构件库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CompWarehouseInfo compWarehouseInfo)
    {
        if (compWarehouseInfo.getBeginFloor()==null && compWarehouseInfo.getEndFloor()==null){

        }else if (compWarehouseInfo.getBeginFloor()==0 && compWarehouseInfo.getEndFloor()==0){
            compWarehouseInfo.setBeginFloor(null);
            compWarehouseInfo.setEndFloor(null);
        }else if (compWarehouseInfo.getBeginFloor()< compWarehouseInfo.getEndFloor()){
            throw new ServiceException("开始楼层不能大于结束楼层");
        }


        if (compWarehouseInfo.getBeginWarehousedTime()==null && compWarehouseInfo.getEndWarehousedTime()==null){

        }else if (compWarehouseInfo.getBeginWarehousedTime().compareTo(compWarehouseInfo.getEndWarehousedTime())<0){
            throw new ServiceException("开始入库时间不能大于开始入库时间");
        }
        List<CompWarehouseInfo> list = compWarehouseInfoService.selectCompWarehouseInfoList(compWarehouseInfo);
        ExcelUtil<CompWarehouseInfo> util = new ExcelUtil<CompWarehouseInfo>(CompWarehouseInfo.class);
        util.exportExcel(response, list, "构件库存信息数据");
    }

    /**
     * 获取构件库存信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('yard:compWarehouseInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(compWarehouseInfoService.selectCompWarehouseInfoById(id));
    }

    /**
     * 新增构件库存信息
     */
    @PreAuthorize("@ss.hasPermi('yard:compWarehouseInfo:add')")
    @Log(title = "构件库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompWarehouseInfo compWarehouseInfo)
    {
        return toAjax(compWarehouseInfoService.insertCompWarehouseInfo(compWarehouseInfo));
    }

    /**
     * 修改构件库存信息
     */
    @PreAuthorize("@ss.hasPermi('yard:compWarehouseInfo:edit')")
    @Log(title = "构件库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompWarehouseInfo compWarehouseInfo)
    {
        return toAjax(compWarehouseInfoService.updateCompWarehouseInfo(compWarehouseInfo));
    }

    /**
     * 删除构件库存信息
     */
    @PreAuthorize("@ss.hasPermi('yard:compWarehouseInfo:remove')")
    @Log(title = "构件库存信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(compWarehouseInfoService.deleteCompWarehouseInfoByIds(ids));
    }
    /**
     * 查询构件库存信息列表-统计
     */
    @PreAuthorize("@ss.hasPermi('yard:compWarehouseInfo:statistics')")
    @GetMapping("/statistics")
    public AjaxResult statistics(CompWarehouseInfo compWarehouseInfo)
    {

        List<CompWarehouseInfo> list = compWarehouseInfoService.selectCompWarehouseInfoList(compWarehouseInfo);
        Long count=0L;
        Double weight=0D;
        for (CompWarehouseInfo c:list){
            count=count+c.getStockCount();
            weight=weight+c.getNetWeight()*c.getStockCount();
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("count",count);
        ajaxResult.put("weight",weight);
        return ajaxResult;
    }

}
